package com.ruoyi.backend.controller;

import com.ruoyi.backend.domain.Order;
import com.ruoyi.backend.domain.OrderStage;
import com.ruoyi.backend.service.IOrderService;
import com.ruoyi.backend.service.IOrderStageService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单款项Controller
 * 
 * @author wjr
 * @date 2025-09-23
 */
@RestController
@RequestMapping("/backend/t_order_stage")
@Api(tags = "订单款项")
public class OrderStageController extends BaseController
{
    @Autowired
    private IOrderStageService orderStageService;
    @Autowired
    private IOrderService orderService;

    /**
     * 查询订单款项列表
     */
    @PreAuthorize("@ss.hasPermi('backend:t_order_stage:list')")
    @GetMapping("/list")
    @ApiOperation("查询订单款项列表")
    public TableDataInfo list(OrderStage orderStage)
    {
        startPage();
        List<OrderStage> list = orderStageService.selectOrderStageList(orderStage);
        return getDataTable(list);
    }

    /**
     * 获取订单款项详细信息
     */
    @PreAuthorize("@ss.hasPermi('backend:t_order_stage:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取订单款项详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(orderStageService.selectOrderStageById(id));
    }

    /**
     * 新增订单款项
     */
    @PreAuthorize("@ss.hasPermi('backend:t_order_stage:add')")
    @Log(title = "订单款项", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增订单款项")
    public AjaxResult add(@RequestBody OrderStage orderStage)
    {
        //添加订单款项中订单相关字段
        Integer orderId = orderStage.getOrderId();
        if (orderId == null) {
            return AjaxResult.error("未获取到订单id");
        }
        Order order = orderService.selectOrderById(orderId);
        if (order == null) {
            return AjaxResult.error("订单不存在");
        }
        orderStage.setTaskName(order.getTaskName());
        orderStage.setShopId(order.getShopId());
        orderStage.setShopName(order.getShopName());
        orderStage.setCustomerId(order.getCustomerId());
        orderStage.setCustomerName(order.getCustomerName());
        orderStage.setChargeId(order.getChargeId());
        orderStage.setChargeName(order.getChargeName());

        return toAjax(orderStageService.insertOrderStage(orderStage));
    }

    /**
     * 修改订单款项
     */
    @PreAuthorize("@ss.hasPermi('backend:t_order_stage:edit')")
    @Log(title = "订单款项", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改订单款项")
    public AjaxResult edit(@RequestBody OrderStage orderStage)
    {
        return toAjax(orderStageService.updateOrderStage(orderStage));
    }

    /**
     * 删除订单款项
     */
    @PreAuthorize("@ss.hasPermi('backend:t_order_stage:remove')")
    @Log(title = "订单款项", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除订单款项")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(orderStageService.deleteOrderStageByIds(ids));
    }

    /**
     * 导出订单款项列表
     */
   /* @PreAuthorize("@ss.hasPermi('backend:t_order_stage:export')")
    @Log(title = "订单款项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderStage orderStage)
    {
        List<OrderStage> list = orderStageService.selectOrderStageList(orderStage);
        ExcelUtil<OrderStage> util = new ExcelUtil<OrderStage>(OrderStage.class);
        util.exportExcel(response, list, "订单款项数据");
    }*/

}
